import Cookies from "js-cookie";

const app = {
  state: {
    sidebar: {
      opened: false,
      withoutAnimation: true
    },
    menu: [],
    currentMenuArr: [],
    appId: "",
    loadingMessage: "处理中,请稍等...",
    pageLoading: {
      pageLoadingStatus: false,
      pageLoadingEnable: false
    },
    isShowSimpleFooter: false,
    routerPower: {
      isShow: false,
      message: ""
    },
    breadcrumb: {}
  },
  mutations: {
    SET_LOADINGMESSAGE: (state, loadingMessage) => {
      state.loadingMessage = loadingMessage;
    },
    SET_PAGELOADING: (state, status) => {
      state.pageLoading.pageLoadingStatus = status;
    },
    SET_PAGELOADINGENABLE: (state, status) => {
      state.pageLoading.pageLoadingEnable = status;
    },
    SET_ROUTER_305: (state, routerPower) => {
      state.routerPower = routerPower;
    },
    SET_BREADCRUMB: (state, dataTemp) => {
      let breadcrumbTemp =
        JSON.parse(
          !JSON.stringify(state.breadcrumb) ||
            localStorage.getItem("breadcrumb")
        ) || {};
      if (Object.prototype.toString.call(dataTemp) === "[object Array]") {
        dataTemp.forEach(item => {
          breadcrumbTemp[item.level] = item.data;
        });
      } else {
        let { level, data } = dataTemp;
        breadcrumbTemp[level] = data;
      }
      localStorage.setItem("breadcrumb", JSON.stringify(breadcrumbTemp));
      state.breadcrumb = breadcrumbTemp;
    },
    SET_APP_ID: (state, appId) => {
      state.appId = appId;
    },
    OPEN_SIDEBAR_TEMPORARY: state => {
      state.sidebar.opened = true;
    },
    CLOSE_SIDEBAR_TEMPORARY: state => {
      state.sidebar.opened = false;
    },
    CLOSE_SIDEBAR: (state, withoutAnimation) => {
      Cookies.set("sidebarStatus", 0);
      state.sidebar.opened = false;
      state.sidebar.withoutAnimation = withoutAnimation;
    },
    SET_MENU: (state, menu) => {
      state.menu = menu;
    },
    SET_CURRENT_MENU_ARR: (state, currentMenuArr) => {
      state.currentMenuArr = currentMenuArr;
    },
    SET_SHOW_SIMPLE_FOOTER: (state, isShowSimpleFooter) => {
      state.isShowSimpleFooter = isShowSimpleFooter;
    }
  },
  actions: {
    setAppId({ commit }, appId) {
      commit("SET_APP_ID", appId);
    },
    closeSidebar({ commit }, { withoutAnimation }) {
      commit("CLOSE_SIDEBAR", withoutAnimation);
    },
    openSidebarTemporary({ commit }) {
      commit("OPEN_SIDEBAR_TEMPORARY");
    },
    closeSidebarTemporary({ commit }) {
      commit("CLOSE_SIDEBAR_TEMPORARY");
    },
    setMenu({ commit }, menu) {
      commit("SET_MENU", menu);
    },
    setCurrentMenuArr({ commit }, currentMenuArr) {
      commit("SET_CURRENT_MENU_ARR", currentMenuArr);
    },
    setPageLoading: ({ commit }, status) => {
      commit("SET_PAGELOADING", status);
    },
    setPageLoadingEnable: ({ commit }, status) => {
      commit("SET_PAGELOADINGENABLE", status);
    },
    setRouter305: ({ commit }, routerPower) => {
      commit("SET_ROUTER_305", routerPower);
    },
    setShowSimpleFooter: ({ commit }, isShowSimpleFooter) => {
      commit("SET_SHOW_SIMPLE_FOOTER", isShowSimpleFooter);
    },
    setBreadcrumb: ({ commit }, data) => {
      commit("SET_BREADCRUMB", data);
    }
  }
};

export default app;
